package com.wudimeng.syll.service.impl;


import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.wudimeng.syll.common.ApiResponse;
import com.wudimeng.syll.constants.Constant;
import com.wudimeng.syll.dao.ICRuKuXinXiDAO;
import com.wudimeng.syll.dao.ICRuKuXinXiShenHeDAO;
import com.wudimeng.syll.dao.ICRuKuXinXiDAO;
import com.wudimeng.syll.entity.CRuKuXinXi;
import com.wudimeng.syll.entity.CRuKuXinXiShenHe;
import com.wudimeng.syll.service.ICRuKuXinXiShenHeService;
import com.wudimeng.syll.utils.ConvertBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.util.List;
import java.util.Map;


@Service
public class CRuKuXinXiShenHeService implements ICRuKuXinXiShenHeService {

    @Autowired
    private ICRuKuXinXiShenHeDAO dao;
    @Autowired
    private ICRuKuXinXiDAO rukudao;

    @Override
    public ApiResponse selectCRuKuXinXiShenHe(Map<String, String> paramMap,Integer pageNum,Integer pageSize) {
        PageHelper.startPage(pageNum,pageSize);
        List<Map<String, Object>> maps = dao.selectCRuKuXinXiShenHe(paramMap);
        PageInfo<Map<String, Object>> pageInfo = new PageInfo<>(maps);
        return ApiResponse.instance(pageInfo);
    }

    @Override
    public ApiResponse selectCRuKuXinXiShenHeById(Integer id) {
        return ApiResponse.instance(dao.selectCRuKuXinXiShenHeById(id));
    }

    @Override
    @Transactional
    public ApiResponse addCRuKuXinXiShenHe(CRuKuXinXiShenHe ruKuXinXiShenHe) {
        try {
            dao.addCRuKuXinXiShenHe(ruKuXinXiShenHe);
        } catch (Exception e) {
            e.printStackTrace();
            return ApiResponse.error(Constant.COMMON_INSERT_FAIL);
        }
        return ApiResponse.success(Constant.COMMON_INSERT_SUCCESS);
    }

    @Override
    @Transactional
    public void updCRuKuXinXiShenHe(CRuKuXinXiShenHe ruKuXinXiShenHe) {
        String shenHeZhuangTai = ruKuXinXiShenHe.getShenHeZhuangTai();
        CRuKuXinXi ruKuXinXi = new CRuKuXinXi();
        dao.updCRuKuXinXiShenHe(ruKuXinXiShenHe);
        if("1".equals(shenHeZhuangTai)){ //审核通过  入库表新增记录
            CRuKuXinXiShenHe ruKuXinXiShenHetemp = dao.selectCRuKuXinXiShenHeById(ruKuXinXiShenHe.getId());
            ruKuXinXi = (CRuKuXinXi)ConvertBean.coverBean2Bean(ruKuXinXiShenHetemp, ruKuXinXi);
            long l = System.currentTimeMillis();//当前时间戳
            ruKuXinXi.setLiuShuiHao(Long.toString(l));
            rukudao.addCRuKuXinXi(ruKuXinXi);
            Map<String, Object> map = rukudao.selectCRuKuXinXiByLiuShuiHao(Long.toString(l),null);
            ruKuXinXiShenHe.setLiuSHuiID((Integer) map.get("LiuSHuiID"));
            ruKuXinXiShenHe.setLiuShuiHao(Long.toString(l));
        }
    }
}
